package com.sugar.subscribe.course.module.business.controller.app.coach;

import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.subscribe.course.module.business.controller.admin.venue.vo.VenueSimpleRespVO;
import com.sugar.subscribe.course.module.business.controller.app.coach.vo.*;
import com.sugar.subscribe.course.module.business.controller.app.serviceclass.vo.AppServiceClassRespVO;
import com.sugar.subscribe.course.module.business.service.coach.CoachService;
import com.sugar.subscribe.course.module.system.api.auth.dto.AppAuthDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import java.util.List;

import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 教练信息")
@RestController
@RequestMapping("/biz/coach")
@Validated
public class AppCoachController {

    @Resource
    private CoachService coachService;

    @PostMapping("/apply")
    @Operation(summary = "用户端 - 教练入驻并登录（未入驻情况）")
    public CommonResult<AppAuthDTO> coachApply(@RequestBody @Valid AppBizUserCoachApplyReqVO coachApplyReqVO) {
        return success(coachService.coachApply(coachApplyReqVO, SecurityFrameworkUtils.getLoginUserId(false)));
    }

    @PostMapping("/login")
    @Operation(summary = "用户端 - 教练登录（已入驻情况）")
    public CommonResult<AppAuthDTO> login() {
        return success(coachService.login(SecurityFrameworkUtils.getLoginUserId(false)));
    }

    @PostMapping("/update")
    @Operation(summary = "教练端 - 更新")
    public CommonResult<Boolean> updateCoach(@Valid @RequestBody AppBizUserCoachApplyReqVO updateReqVO) {
        coachService.appUpdateCoach(updateReqVO, SecurityFrameworkUtils.getLoginUserId(true));
        return success(true);
    }

    @PostMapping("/update/work")
    @Operation(summary = "教练端 - 更新工作状态")
    public CommonResult<Boolean> updateCoachWork(@Valid @RequestBody AppBizCoachUpdateWorkReqVO workReqVO) {
        coachService.updateCoachWork(workReqVO.getIsWork(), SecurityFrameworkUtils.getLoginUserId(true));
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "教练端 - 查详情")
    public CommonResult<AppBizCoachRespVO> getCoach() {
        return success(coachService.appGetCoach(SecurityFrameworkUtils.getLoginUserId(true)));
    }

    @GetMapping("/home/page")
    @Operation(summary = "用户端 - 分页查询教练（首页与分类列表页共用）")
    @PermitAll
    public CommonResult<PageResult<AppBizCoachHomePageRespVO>> getCoachHomePage(@Valid AppBizCoachHomePageReqVO reqVO) {
        return success(coachService.getCoachHomePage(reqVO));
    }

    @GetMapping("/get/{coachId}")
    @Operation(summary = "用户端 - 查详情")
    @PermitAll
    public CommonResult<AppBizCoachDetailRespVO> getCoachDetail(@PathVariable("coachId") Long coachId) {
        return success(coachService.appGetCoachDetail(coachId));
    }

    @GetMapping("/get/service/class/{coachId}")
    @Operation(summary = "公共 - 获取教练下的所有服务分类（树形）")
    public CommonResult<List<AppServiceClassRespVO>> getCoachServiceClassListByCoachId(@PathVariable("coachId")Long coachId) {
        return success(coachService.getCoachServiceClassTreeByCoachId(coachId));
    }

    @GetMapping("/get/venue")
    @Operation(summary = "公共 - 获取教练下的场地")
    public CommonResult<List<VenueSimpleRespVO>> getCoachVenueListByCoachId(@RequestParam("coachId")Long coachId, @RequestParam(value = "cityId", required = false)Long cityId) {
        return success(coachService.getCoachVenueListByCoachId(coachId, cityId));
    }
}